var express = require('express');
var router = express.Router();
var workflowdao = require('../dao/workflowdao');
var taskdao = require('../dao/taskdao');

router.post('/gettaskitem', function (req, res, next) {
    var userid = req.session.user.userid;
    var dsid = req.body.dsid;
    var actions = req.body.actions || '1';
    if (dsid == undefined || dsid == "") {
        return res.json({
            code: 0,
            msg: 'dsid null'
        });
    }
    var params = {
        userid: userid,
        dsid: dsid,
        tablename: 'dstable_' + dsid,
        tasknumber: (actions == '1' ? 9 : 1)
    };

    workflowdao.gettaskitem(params).then(function (data) {
        //console.log(data);

        if (data) {
            return res.json({
                code: 1,
                data: data[0][1][0]
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

router.post('/marksubmit', function (req, res, next) {
    var userid = req.session.user.userid;
    var dsid = req.body.dsid;
    var taskid = req.body.taskid;
    var selectedids = req.body.selectedids;
    var unselectedids = req.body.unselectedids;
    var spent = req.body.spent;
    var currentflow = req.body.currentflow; //当前流程名 mark inspect rndinspect accept
    var actions = req.body.actions || '1'; //标注引擎(1=数据清洗 2=单分类 3=多分类 4=多点标注 )
    var selected_tag = req.body.selected_tag; //选中的标签
    taskdao.gettaskstatus(taskid).then(function (task) {
        if (task[0].status == 1) { //进行中的任务才能被提交
            var marklogs = [];
            selectedids = JSON.parse(selectedids);
            unselectedids = JSON.parse(unselectedids);
            var yes_label, no_label;
            switch (actions) {
                case '1':
                    yes_label = {
                        outputs: [{
                            'name': '##classify##'
                        }]
                    };
                    no_label = {
                        outputs: [{
                            'name': '##unclassify##'
                        }]
                    };
                    break;
                case '2':
                case '3':
                    selected_tag = JSON.parse(selected_tag);
                    var tags = [];
                    selected_tag.forEach(tag => {
                        tags.push({
                            'name': tag
                        });
                    });
                    yes_label = {
                        outputs: tags
                    };
                    no_label = {
                        outputs: tags
                    };
                    break;
                case '4':
                    break;
                default:
                    yes_label = {
                        outputs: [{
                            'name': '##classify##'
                        }]
                    };
                    no_label = {
                        outputs: [{
                            'name': '##unclassify##'
                        }]
                    };
                    break;
            }
            var params = {
                userid: userid,
                dsid: dsid,
                tablename: 'dstable_' + dsid,
                selectedids: selectedids,
                unselectedids: unselectedids,
                spent: spent,
                taskid: taskid,
                yes_label: JSON.stringify(yes_label),
                no_label: JSON.stringify(no_label),
                actions: actions //标注引擎(1=数据清洗 2=单分类 3=多分类 4=多点标注 )
            };

            var resource_count = selectedids.length + unselectedids.length;
            var single_spent = Math.ceil(spent / resource_count); //向上取整
            //marklog actions字段含义：动作（1=标注 2=检查 3=抽查）

            selectedids.forEach(resourceid => {
                var log = {};
                log.userid = userid;
                log.resourceid = resourceid;
                log.taskid = taskid;
                log.spent = single_spent;
                switch (currentflow) {
                    case 'mark':
                        log.actions = 1; //标注
                        break;
                    case 'inspect':
                        log.actions = 2; //检查
                        log.result = 0;
                        break;
                    case 'rndinspect':
                        log.actions = 3; //抽查
                        log.result = 0;
                        break;
                    default:
                        log.actions = 1;
                        break;
                }
                marklogs.push(log);
            });

            unselectedids.forEach(resourceid => {
                var log = {};
                log.userid = userid;
                log.resourceid = resourceid;
                log.taskid = taskid;
                log.spent = single_spent;
                switch (currentflow) {
                    case 'mark':
                        log.actions = 1; //标注
                        break;
                    case 'inspect':
                        log.actions = 2; //检查
                        log.result = 1;
                        break;
                    case 'rndinspect':
                        log.actions = 3; //抽查
                        log.result = 1;
                        break;
                    default:
                        log.actions = 1;
                        break;
                }
                marklogs.push(log);
            });
            workflowdao.marksubmit(marklogs, params, currentflow).then(function (data) {
                //console.log(data);

                try {
                    if (data) {
                        return res.json({
                            code: 1,
                            data: data[0][0][0]
                        });
                    } else {
                        return res.json({
                            code: 0
                        });
                    }
                } catch (error) {
                    console.error(error);
                    return res.json({
                        code: 0
                    });
                }
            });
        } else {
            return res.json({
                code: -1,
                msg: '当前任务已验收'
            });
        }
    }, function (err) {
        return res.json({
            code: 0,
            msg: '任务状态获取失败'
        });
    });



});

//数据筛选 检查  数据获取
router.post('/gettaskitem_inspect', function (req, res, next) {
    var userid = req.session.user.userid;
    var dsid = req.body.dsid;
    var actions = req.body.actions || '1';
    var tag = req.body.tag || '';
    if (dsid == undefined || dsid == "") {
        return res.json({
            code: 0,
            msg: 'dsid null'
        });
    }
    var params = {
        userid: userid,
        dsid: dsid,
        tablename: 'dstable_' + dsid,
        tasknumber: (actions == '1' ? 9 : 1),
        tag: tag
    };

    workflowdao.gettaskitem_inspect(params).then(function (data) {
        //console.log(data);

        if (data) {
            return res.json({
                code: 1,
                data: data[0][1][0]
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

//数据筛选 检查  数据获取
router.post('/gettaskitem_rndinspect', function (req, res, next) {
    var userid = req.session.user.userid;
    var dsid = req.body.dsid;
    var actions = req.body.actions || '1';
    var tag = req.body.tag || '';
    if (dsid == undefined || dsid == "") {
        return res.json({
            code: 0,
            msg: 'dsid null'
        });
    }
    var params = {
        userid: userid,
        dsid: dsid,
        tablename: 'dstable_' + dsid,
        tasknumber: (actions == '1' ? 12 : 1),
        tag: tag
    };

    workflowdao.gettaskitem_rndinspect(params).then(function (data) {
        //console.log(data);

        if (data) {
            return res.json({
                code: 1,
                data: data[0][1][0],
                percent: data[0][2][0][0].percent
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});
module.exports = router;